package com.example.remindme.RemindModule.DataCard.ViewHolder

import ViewHolderLine
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.remindme.R

val IS_LINE_ELEM=101
val IS_BLOCK_ELEM=102
val IS_LIST_VIEW=103

class ViewAdapter(private val dataList: List<RecyclerData>): RecyclerView.Adapter<RecyclerView.ViewHolder> ()
{
    override fun getItemViewType(position: Int): Int {
        val item=dataList[position]
        if(item is Matter)
        {

            if(item.timed>0)  // 说明有时间标记，则访问block
                return IS_BLOCK_ELEM
            else
                return IS_LINE_ELEM
        }
        else
            return IS_LIST_VIEW
    }

    // 1, 定义一个接口
    interface onRecyclerViewItemClickListener{
        fun onItemClick(view: View, position:Int)
    }
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!
    lateinit var ClickListener: onRecyclerViewItemClickListener
    fun setOnItemClick(listener: onRecyclerViewItemClickListener)
    {
        this.ClickListener=listener
    }
    //  声明一个接口变量
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater= LayoutInflater.from(parent.context)
        // 用同一个inflater加载不同的vieholder
        val viewLine=inflater.inflate(
                    R.layout.line_item2, parent,
                    false
            )
        val lineViewHolder= ViewHolderLine(viewLine)

        val viewBlock=inflater.inflate(
                R.layout.block_item, parent,
                false
        )// 此处的layout是指，对应的元素的模板要从哪个地方加载
        val blockViewHolder= ViewHolderBlock(viewBlock)

        val viewList=inflater.inflate(
                R.layout.list_item,parent,
                false
        )
        val listViewHolder=ViewHolderList(viewList)






        if(viewType== IS_LIST_VIEW){
            viewList.setOnClickListener{
                ClickListener.onItemClick(it,it.getTag() as Int)
            }
        }
        else if(viewType== IS_BLOCK_ELEM){
            viewBlock.setOnClickListener {
                Log.d("data","block!!!!!!!!!!!!!!")
                ClickListener.onItemClick(it, it.getTag() as Int)
            }
        }
        else {
            viewLine.setOnClickListener {
                Log.d("data","line!!!!!!!!!!!!!!")
                ClickListener.onItemClick(it, it.getTag() as Int)
            }
        }

       return when (viewType)
        {
            IS_LINE_ELEM->lineViewHolder
            IS_BLOCK_ELEM->blockViewHolder
           else->listViewHolder
        }

    }
    // !!!!!!!!!!!!!!!!!!!,提供一个set,设置当前的接口对象



    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if(position<dataList.size)
        {
            val item=dataList[position]
            if(holder is ViewHolderLine)
            { holder.bind(item)
                // !!!!!!!!!!!!!!!!!!!
                holder.itemView.setTag(position)
            }

            else if(holder is ViewHolderBlock)
            {
                holder.bind(item)
                holder.itemView.setTag(position)
            }
            else if(holder is ViewHolderList)
            {
                holder.bind(item)
                holder.itemView.setTag(position)
            } 
        }


    }
    override fun getItemCount(): Int {
    return dataList.size
    }

}